package com.winit.openapi.oauth.validator;

import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

public class APIOAuth2Validator {

    public static void validateClientAdd(JSONObject json) {
        ValidateUtil.validNotNull(json, "clientName");
        ValidateUtil.validMaxLength(json, "clientName", "Client name", 35, true);
        // 应用名称只能包含数字、字母、下划线，中划线，中文
        validClientName(json.getString("clientName"));
        ValidateUtil.validNotNull(json, "clientCode");
        ValidateUtil.validMaxLength(json, "clientCode", 20, true);
        ValidateUtil.validNotNull(json, "type");
        ValidateUtil.validByRegex(json, "type", "[OP]", true);
        
        ValidateUtil.validNotNull(json, "isDefaultURL");
        String isDefaultURL = json.getString("isDefaultURL");
        if ("N".equals(isDefaultURL)) {
            ValidateUtil.validNotNull(json, "redirectURL");
            ValidateUtil.validMaxLength(json, "redirectURL", 1000, true);
            validRedirectUrl(json.getString("redirectURL"));
        }
        
        ValidateUtil.validNotNull(json, "developerAccount");
        ValidateUtil.validMaxLength(json, "developerAccount", 100, true);
        if (json.containsKey("comment")) {
            ValidateUtil.validMaxLength(json, "comment", 500, false);
        }

    }

    public static void validateClientUpdate(JSONObject json) {
        ValidateUtil.validNotNull(json, "clientId");
        ValidateUtil.validMaxLength(json, "clientId", 100, true);
        ValidateUtil.validNotNull(json, "clientName");
        ValidateUtil.validMaxLengthTrim(json, "clientName", "Client name", 35, false);
        validClientName(json.getString("clientName"));
        ValidateUtil.validNotNull(json, "clientCode");
        ValidateUtil.validMaxLengthTrim(json, "clientCode", "Client Code", 20, false);
        ValidateUtil.validNotNull(json, "type");
        ValidateUtil.validByRegex(json, "type", "[OP]", true);
        ValidateUtil.validMaxLengthTrim(json, "logoImageUri", "logoImageUri", 256, false);
        ValidateUtil.validMaxLengthTrim(json, "authUrl", "auth Url", 1000, false);
        ValidateUtil.validNotNull(json, "isDefaultURL");
        String isDefaultURL = json.getString("isDefaultURL");
        if ("N".equals(isDefaultURL)) {
            ValidateUtil.validMaxLength(json, "redirectURL", 1000, true);
            ValidateUtil.validMaxLength(json, "redirectURL", 1000, true);
            validRedirectUrl(json.getString("redirectURL"));
        }
        
        
        if (json.containsKey("comment")) {
            ValidateUtil.validMaxLength(json, "comment", 500, false);
        }

    }

    private static void validClientName(String clientName) {
        String regx = "^[a-zA-Z0-9\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5_-]+$";
        if (!clientName.matches(regx)) {
            throw new ApiException(ErrorCode.OAUTH_FIELD_FORMAT_ERROR, "clientName");

        }
    }

    private static void validDeveloperAccount(String developerAccount) {
        String regx = "^[a-zA-Z0-9][a-zA-Z0-9@_.]{3,20}$";
        if (!developerAccount.matches(regx)) {
            throw new ApiException(ErrorCode.OAUTH_FIELD_FORMAT_ERROR, "developerAccount");

        }
    }

    public static void validDeveloperPassword(String password) {
        String regx = "^[a-zA-Z0-9_]{4,10}$";
        if (!password.matches(regx)) {
            throw new ApiException(ErrorCode.OAUTH_FIELD_FORMAT_ERROR, "password");

        }
    }

    public static void validRedirectUrl(String url) {
        Pattern pattern = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
        if (!pattern.matcher(url).matches()) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "redirectUrl", "回调URL");
        }
    }

    public static void validateDeveloperAdd(JSONObject json) {

        ValidateUtil.validNotNull(json, "type");
        ValidateUtil.validByRegex(json, "type", "[OP]", true);
        // 当账户类型为”平台开发者“时才显示，并要求必填写
        if ("O".equals(json.getString("type"))) {
            ValidateUtil.validNotNull(json, "platformName");

        }

        if (json.containsKey("platformName")) {
            ValidateUtil.validMaxLength(json, "platformName", "platformName", 100, false);
        }

        ValidateUtil.validNotNull(json, "mode");
        ValidateUtil.validByRegex(json, "mode", "default|winit", true);
        String mode = json.getString("mode");
        // 开发者自主注册需要提供账号和密码
        if ("default".equals(mode)) {
            ValidateUtil.validNotNull(json, "developerAccount");
            ValidateUtil.validMaxLength(json, "developerAccount", "developerAccount", 100, true);
            // 应用名称只能包含数字、字母、下划线，中划线
            validDeveloperAccount(json.getString("developerAccount"));

            ValidateUtil.validNotNull(json, "password");
            ValidateUtil.validMaxLength(json, "password", "password", 10, true);
            validDeveloperPassword(json.getString("password"));
        } else if ("winit".equals(mode)) {// 当是WINIT模式注册时候developerAccount即使万邑联登录账号
            ValidateUtil.validNotNull(json, "developerAccount");
            ValidateUtil.validMaxLength(json, "developerAccount", "developerAccount", 100, true);
        } else {
            throw new ApiException(ErrorCode.OAUTH_DEVELOPER_LOGIN_MODE_ERROR);
        }

        if (json.containsKey("contactPersonName")) {
            ValidateUtil.validMaxLength(json, "contactPersonName", 50, false);
        }
        ValidateUtil.validNotNull(json, "contactPersonMobile");
        ValidateUtil.validMaxLength(json, "contactPersonMobile", 20, true);
        ValidateUtil.validMaxLength(json, "email", 100, false);
        ValidateUtil.validEmail(json, "email");

    }

    public static void validateDeveloperUpdate(JSONObject json) {
        ValidateUtil.validNotNull(json, "id");
        ValidateUtil.validMaxLength(json, "id", 10, true);
        ValidateUtil.validMustNumber(json, "id");
        ValidateUtil.validNotNull(json, "type");
        ValidateUtil.validByRegex(json, "type", "[OP]", true);
        // 平台开发者才有平台名称属性
        if ("O".equals(json.getString("type"))) {
            ValidateUtil.validNotNull(json, "platformName");
            ValidateUtil.validMaxLength(json, "platformName", "platformName", 100, false);
        }

        // 应用名称只能包含数字、字母、下划线，中划线
        ValidateUtil.validNotNull(json, "developerAccount");
        ValidateUtil.validMaxLength(json, "developerAccount", "developerAccount", 100, true);
        validDeveloperAccount(json.getString("developerAccount"));

        // ValidateUtil.validNotNull(json, "password");
        // ValidateUtil.validMaxLength(json, "password", "password", 10, true);
        // validDeveloperPassword(json.getString("password"));

        if (json.containsKey("contactPersonName")) {
            ValidateUtil.validMaxLength(json, "contactPersonName", 50, false);
        }
        ValidateUtil.validNotNull(json, "contactPersonMobile");
        ValidateUtil.validMaxLength(json, "contactPersonMobile", 20, true);
        ValidateUtil.validMaxLength(json, "email", 100, false);
        ValidateUtil.validEmail(json, "email");

    }

    public static void main(String[] args) {
        validDeveloperAccount("abcde");
    }

}
